package dao;

import gui.GameMenuBar;

import java.io.File;
import java.io.IOException;

import org.xml.sax.SAXException;

import data.Data.TeamID;
import data.Team;

/**
 * Data loader.
 * 
 * @author Tomasz Dąbrowski
 * 
 */
public class DataLoader {

	/**
	 * Loaded team A.
	 */
	private Team teamA;

	/**
	 * Loaded team B.
	 */
	private Team teamB;

	/**
	 * Instance.
	 */
	private static DataLoader instance;

	/**
	 * Team files paths.
	 */
	private String[] filePaths = { "data/DefaultOrcTeam.xml", "data/DefaultHumanTeam.xml" };

	/**
	 * Creates a new instance.
	 */
	private DataLoader() {
	}

	/**
	 * Getter for the "instance" property.
	 * 
	 * @return the instance
	 */
	public static DataLoader getInstance() {
		if (instance == null) {
			instance = new DataLoader();
		}
		return instance;
	}

	/**
	 * Loads a team.
	 * 
	 * @param id
	 *            team id
	 * @param file
	 *            file
	 * @return loaded team
	 */
	public final Team loadTeam(final TeamID id, final File file) {
		Team loadedTeam = null;

		try {
			loadedTeam = new Team(file);
		} catch (IOException e) {
			GameMenuBar.getInstance().showMessage("The file couldn't be opened successfully.", "File open error", true);
		} catch (SAXException e) {
			GameMenuBar.getInstance().showMessage("The file doesn't contain a valid team, or it is corrupted.", "Team load error", true);
		}

		if (loadedTeam != null) {
			loadedTeam.resetToDefault();
			if (loadedTeam.isTeamReadyToPlay()) {
				loadedTeam.setId(id);
				GameMenuBar.getInstance().setSelectedTeam(id, loadedTeam);
			} else {
				GameMenuBar.getInstance().showMessage("The team doesn't have enough players to play (minimum 11 players).",
						"Team selection error", true);
			}
		}
		try {
			return loadedTeam;
		} finally {
			switch (id) {
			case TEAM_A:
				teamA = loadedTeam;
				break;
			case TEAM_B:
				teamB = loadedTeam;
				break;
			}
		}
	}

	/**
	 * Loads default teams.
	 * 
	 * @throws RuntimeException
	 *             general failure exception
	 */
	public void loadDefaultTeams() throws RuntimeException {
		if (teamA == null) {
			loadTeam(TeamID.TEAM_A, new File(getFilePaths()[0]));
		}
		if (teamB == null) {
			loadTeam(TeamID.TEAM_B, new File(getFilePaths()[1]));
		}
		teamA.resetToDefault();
		teamB.resetToDefault();
	}

	/**
	 * Getter for the "filePaths" property.
	 * 
	 * @return the filePaths
	 */
	private String[] getFilePaths() {
		return filePaths;
	}

	/**
	 * Is team auto select.
	 * 
	 * @return team auto select
	 */
	public boolean isTeamAutoSelect() {
		return true;
	}

	/**
	 * Is team A AI controlled.
	 * 
	 * @return team A AI controlled.
	 * 
	 * @author Tomasz Dąbrowski
	 */
	public boolean isTeamAAIControlled() {
		return true;
	}

	/**
	 * Is team B AI controlled.
	 * 
	 * @return team B AI controlled.
	 * 
	 * @author Tomasz Dąbrowski
	 */
	public boolean isTeamBAIControlled() {
		return true;
	}

	/**
	 * Getter for the "teamA" property.
	 * 
	 * @return the teamA
	 */
	public Team getTeamA() {
		return teamA;
	}

	/**
	 * Getter for the "teamB" property.
	 * 
	 * @return the teamB
	 */
	public Team getTeamB() {
		return teamB;
	}

}
